/*
 * $Id: Command.h,v 1.4 2005-07-24 11:39:14 bacon Exp $
 */

#ifndef _COMMAND_H_
#define _COMMAND_H_

#include <xp/bas/string.h>
#include <xp/bas/stdio.h>

#define CMD_OK      XP_TEXT("OK")
#define CMD_ERR     XP_TEXT("ERR")
#define CMD_INFO    XP_TEXT("INFO")
#define CMD_WARN    XP_TEXT("WARN")

#define CMD_DISCON  XP_TEXT("DISCON")   // disconnect
#define CMD_ADMATH  XP_TEXT("ADMATH")   // administrator authentication
#define CMD_SHTDWN  XP_TEXT("SHTDWN")
#define CMD_ENVGET  XP_TEXT("ENVGET")
#define CMD_ENVSET  XP_TEXT("ENVSET")
#define CMD_ENVLST  XP_TEXT("ENVLST")
#define CMD_ENVITM  XP_TEXT("ENVITM")
#define CMD_LOGCLR  XP_TEXT("LOGCLR")
#define CMD_LOGLST  XP_TEXT("LOGLST")
#define CMD_LOGITM  XP_TEXT("LOGITM")

#define SEND_CMD_0(cmd) \
	do { if (sendCmd(cmd,0) == -1) return -1; } while (0)
#define SEND_CMD_1(cmd,msg) \
	do { if (sendCmd(cmd,1,msg) == -1) return -1; } while (0)
#define SEND_CMD_2(cmd,msg1,msg2) \
	do { if (sendCmd(cmd,2,msg1,msg2) == -1) return -1; } while (0)
#define SEND_CMD_3(cmd,msg1,msg2,msg3) \
	do { if (sendCmd(cmd,3,msg1,msg2,msg3) == -1) return -1; } while (0)
#define SEND_CMD_4(cmd,msg1,msg2,msg3,msg4) \
	do { if (sendCmd(cmd,4,msg1,msg2,msg3,msg4) == -1) return -1; } while (0)
#define SEND_CMD_5(cmd,msg1,msg2,msg3,msg4,msg5) \
	do { if (sendCmd(cmd,5,msg1,msg2,msg3,msg4,msg5) == -1) return -1; } while (0)
#define SEND_CMD_6(cmd,msg1,msg2,msg3,msg4,msg5,msg6) \
	do { if (sendCmd(cmd,6,msg1,msg2,msg3,msg4,msg5,msg6) == -1) return -1; } while (0)
#define SEND_CMD_7(cmd,msg1,msg2,msg3,msg4,msg5,msg6,msg7) \
	do { if (sendCmd(cmd,7,msg1,msg2,msg3,msg4,msg5,msg6,msg7) == -1) return -1; } while (0)
#define SEND_CMD_8(cmd,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8) \
	do { if (sendCmd(cmd,8,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8) == -1) return -1; } while (0)
#define SEND_CMD_9(cmd,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9) \
	do { if (sendCmd(cmd,9,msg1,msg2,msg3,msg4,msg5,msg6,msg7,msg8,msg9) == -1) return -1; } while (0)

#define SEND_CMD_OK           SEND_CMD_0(CMD_OK)
#define SEND_CMD_ERR          SEND_CMD_0(CMD_ERR)
#define SEND_OK(msg)          SEND_CMD_1(CMD_OK,msg)
#define SEND_OK_2(msg1,msg2)  SEND_CMD_2(CMD_OK,msg1,msg2)

#define SEND_ERR(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_ERR,__buf__,this->errorStr()); \
	} while (0)

#define SEND_ERR_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_ERR,__buf__,msg); \
	} while (0)

#define SEND_INFO(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_INFO,__buf__,this->errorStr()); \
	} while (0)

#define SEND_INFO_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_INFO,__buf__,msg); \
	} while (0)

#define SEND_WARN(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_WARN,__buf__,this->errorStr()); \
	} while (0)

#define SEND_WARN_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_WARN,__buf__,msg); \
	} while (0)

#endif
